//  计算数组中连续子数组的最大乘积
// O(n)时间复杂度


function maxProduct(nums) {
  if (nums.length === 0) return 0;
  let maxSoFar = nums[0];
  let minSoFar = nums[0];
  let result = nums[0];

  for (let i = 1; i < nums.length; i++) {
    const curr = nums[i];
    const tempMax = Math.max(curr, curr * maxSoFar, curr * minSoFar);
    minSoFar = Math.min(curr, curr * maxSoFar, curr * minSoFar);
    maxSoFar = tempMax;

    result = Math.max(maxSoFar, result);
  }

  return result;
}
